//
//  IncomeViewCell.m
//  NiuBanJinGold
//
//  Created by Billy on 15/10/17.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "IncomeViewCell.h"

@interface IncomeViewCell ()

@property (nonatomic, strong) UILabel *label_left; //时间
@property (nonatomic, strong) UILabel *label_right;// 收益
@property (nonatomic, strong) UILabel *label_space;// 收益后同的空格
@end

@implementation IncomeViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier contentWidth:(CGFloat) contentWidth labelColor:(UIColor *) color {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setBackgroundColor:BASECOLOR];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self subViewsCreation];
    }
    
    return self;
}

#pragma mark - subviews creation

-  (void) subViewsCreation {
    
    // 长度的变化有规则 1, 最长 ， 2 短, 3， 长 ,rest same
    
    UIView *view_top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, MULTIPLYHEIGHT(25.0/2))];
    [view_top setBackgroundColor:BASECOLOR];
    
    UIView *view_parent = [UsefulUtil viewCreationWithFrame:CGRectMake(MULTIPLYWIDTH(30.0/2), MULTIPLYHEIGHT(25.0/2), SCREEN_WIDTH -  MULTIPLYWIDTH(60.0/2), MULTIPLYHEIGHT(70.0/2)) backgroundColor:[UIColor whiteColor] backGroundImage:nil];
    
    [self.contentView addSubview:view_top];
    [self.contentView addSubview:view_parent];
    
    
    self.label_left = [UsefulUtil labelCreationWithFrame:CGRectMake(0, 0, MULTIPLYWIDTH(200.0/2), MULTIPLYHEIGHT(70.0/2)) textColor:[UIColor whiteColor] fontName:[UIFont systemFontOfSize:16] text:nil]; //默认宽度为100.因为在创建cell的时候是不会有数据进来的，所以给个默认值
    
    [view_parent addSubview:self.label_left];
    
    // 右边显示收益的label,因为要在取值的时候就计算出长度,可以将计算的算法拿出去
    
    self.label_right  = [UsefulUtil labelCreationWithFrame:CGRectMake(CGRectGetMaxX(self.label_left.frame) - 0.5, 0, MULTIPLYWIDTH(100.0), MULTIPLYHEIGHT(70.0/2)) textColor:[UIColor whiteColor] fontName:[UIFont systemFontOfSize:16] text:nil];

    [self.label_right setTextAlignment:NSTextAlignmentRight];
    [view_parent addSubview:self.label_right];
    
    [self.label_right setBackgroundColor:[UsefulUtil colorWithHexString:@"#aaaaaa"]];
    [self.label_left setBackgroundColor:[UsefulUtil colorWithHexString:@"#aaaaaa"]];

    self.label_space = [[UILabel alloc] init];
    [self.label_space setBackgroundColor:[UsefulUtil colorWithHexString:@"#aaaaaa"]];
    [view_parent addSubview:self.label_space];
}

- (void)layoutSubviews {
   
    CGFloat width =  [self widthOfDayProfit:self.income.assetList_value];
    
    CGRect frame = self.label_right.frame;
    frame.size.width = width;
    
    [self.label_right setFrame:frame];
    
    // 创建空格
    
    [self.label_space setFrame:CGRectMake(CGRectGetMaxX(self.label_right.frame) - 1.0, 0, MULTIPLYWIDTH(25.0/2), MULTIPLYHEIGHT(70.0/2))];
  
    if (self.color_cell) {
        
        [self.label_left setBackgroundColor:self.color_cell];
        [self.label_right setBackgroundColor:self.color_cell];
        [self.label_space setBackgroundColor:self.color_cell];
    }
    
    [self.label_left setText:[NSString stringWithFormat:@"  %@",self.income.assetList_time]];
    [self.label_right setText:[NSString stringWithFormat:@"%@",self.income.assetList_value]];
}


#pragma mark - width calculation

- (CGFloat) widthOfDayProfit:(NSString *) income { //server返回来的是 +500.00这样的
    
    CGFloat value = [[[income componentsSeparatedByString:@"+"] lastObject] floatValue];
    //除去左右60p和左label剩下的宽度
    
    CGFloat width_max = (SCREEN_WIDTH - MULTIPLYWIDTH(260.0/2)) * 0.9; //用来显示的宽度为90%,不能显示满
    
    // 所 有记录中最高 的一条对应 width,剩下的按比例计算,所以我要先计算出哪一个值最大，但是因为cell的加载机制，必须在进到这个方法的时候就要知道这个值，要不然就会运行50*50*4 次
    
    CGFloat width_real = value * width_max / self.dayProfitMax ;
    return ( width_real> 40) ? width_real : 40 ; //太小了给他40显示
}


@end
